﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Microsoft.Win32;

namespace CSF.WinApp.JobNotifier
{
    public  class Common
    {
        /*
        #region 读写注册表
        private const string _registUserName = "name";
        private const string _registUserPwd = "pwd";
        /// <summary>
        /// 从注册表里读取用户数据
        /// </summary>
        /// <returns></returns>
        public string[] GetRegistData()
        {
            string[] user = new string[] { "", "" };
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey("CSF", true);
            if (aimdir != null)
            {
                user[0] = Convert.ToString(aimdir.GetValue(_registUserName));
                user[1] = Convert.ToString(aimdir.GetValue(_registUserPwd).ToString());
            }
            return user;
        }

        /// <summary>
        /// 把用户数据写入到注册表里
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userPwd">密码</param>
        public void SetRegistData(string userName, string userPwd)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.CreateSubKey("CSF");
            aimdir.SetValue(_registUserName, userName);
            aimdir.SetValue(_registUserPwd, userPwd);
        }

        #endregion
        */

        #region 读写文件

        private const string filePath="c:\\Windows\\user.config";

        /// <summary>
        /// 写入登陆信息到硬盘上
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userPwd">密码</param>
        public void SetUserInfo(string userName,string userPwd)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            XmlTextWriter writer = new XmlTextWriter(filePath, null);
            writer.Formatting = Formatting.Indented;  //缩进格式
            writer.Indentation = 4;

            writer.WriteStartDocument();

            writer.WriteStartElement("User");

            writer.WriteStartElement("Name");
            writer.WriteString(userName);
            writer.WriteEndElement();

            writer.WriteStartElement("Pwd");
            writer.WriteString(userPwd);
            writer.WriteEndElement();

            writer.Flush();
            writer.Close();
        }

        /// <summary>
        /// 读取用户信息
        /// </summary>
        /// <returns>用户信息</returns>
        public string[] GetUserInfo()
        {
            string[] user = new string[2];
            if (!File.Exists(filePath))
            {
                return user;
            }
            XmlTextReader reader = new XmlTextReader(filePath);
            while (reader.Read())
            {
                if (reader.LocalName.Equals("Name"))
                {
                    user[0] = reader.ReadString();
                }
                if (reader.LocalName.Equals("Pwd"))
                {
                    user[1] = reader.ReadString();
                }
            }
            return user;
        }

        public void ClearUserInfo()
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
        #endregion

        #region 开机启动
        /// <summary>
        /// 开机启动
        /// </summary>
        public void StartWithWindows()
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey run = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

            if (run.GetValue("CSF") == null)
            {
                string exeFileName = Application.ExecutablePath;//含可执行文件名
                run.SetValue("CSF", exeFileName);
            }
            else
            {
                run.DeleteValue("CSF");
                string exeFileName = Application.ExecutablePath;//含可执行文件名
                run.SetValue("CSF", exeFileName);
            }
            run.Close();
            hklm.Close();
        }
        #endregion

        public void SetDataGridViewOrder(DataGridView dgvList)
        {
            dgvList.Columns["JobNo"].DisplayIndex = 0;
            dgvList.Columns["ItemCode"].DisplayIndex = 1;
            dgvList.Columns["Quanlity"].DisplayIndex = 2;
            dgvList.Columns["GP1"].DisplayIndex = 3;
            dgvList.Columns["IsUrgent"].DisplayIndex = 4;
            dgvList.Columns["Step1"].DisplayIndex = 5;
            dgvList.Columns["Step2"].DisplayIndex = 6;
            dgvList.Columns["Step3"].DisplayIndex = 7;
            dgvList.Columns["Step4"].DisplayIndex = 8;
            dgvList.Columns["Step5"].DisplayIndex = 9;
            dgvList.Columns["Comment"].DisplayIndex = 10;
        }
    }
}
